package com.woniu.bh.filter;

import com.woniu.bh.util.JwtUtil;
import io.jsonwebtoken.ExpiredJwtException;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @version: V1.0
 * @author: Xbao
 * @className: LoginFilter
 * @packageName: com.woniu.bh.filter
 * @description:
 * @data: 2020-10-14 14:49
 */
@Component
public class LoginFilter implements GlobalFilter, Ordered {


    private static List<String> releaseUri = Arrays.asList(
            "/back/manager/login",
            "/back/manager/getcode",
            "/back/manager/verify",
            "/back/manager/modifypwd",

            "/customer/account/phoneLogin",
            "/customer/account/getMessageCode",
            "/customer/account/getMailCode",
            "/customer/account/phoneRegister",
            "/customer/account/emailRegister",
            "/customer/account/emailLogin",
            "/customer/account/recoverPwdByPhone",
            "/customer/account/recoverPwdByEmail",
            "/customer/pay/payOrder",

            "/beautysalon/user/loginByPass",
            "/beautysalon/user/register",
            "/beautysalon/user/getCode",
            "/beautysalon/user/loginByMsg",
            "/beautysalon/user/restPass",

            "/customer/v2/api-docs",
            "/back/v2/api-docs",
            "/beautysalon/v2/api-docs",
            "swagger-ui.html"
    );

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //拿到请求地址
        String url = exchange.getRequest().getURI().toString();
        String uri = exchange.getRequest().getPath().toString();
        System.out.println("请求地址"+url);
        System.out.println("资源定位"+uri);
        //判断是否是登陆路径
        if(releaseUri.contains(uri)) {
            return chain.filter(exchange);
        }
        //取出token并判断
        String token = exchange.getRequest().getHeaders().getFirst("X-Token");
        if(StringUtils.isNotEmpty(token)){
            try {
                JwtUtil.parseToken(token);
                //通过
                return chain.filter(exchange);
            }catch (ExpiredJwtException | SecurityException e){
                e.printStackTrace();
                exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
                return exchange.getResponse().writeWith(Mono.fromSupplier(()->{
                    return exchange.getResponse().bufferFactory().wrap("{\"resp\":\"没有登录\"}".getBytes());
                }));
            }
        }
        return exchange.getResponse().writeWith(Mono.fromSupplier(()->{
            return exchange.getResponse().bufferFactory().wrap("{\"resp\":\"没有登录\"}".getBytes());
        }));
    }

    @Override
    public int getOrder() {
        //值越小先处理
        return 1;
    }
}
